package com.qifenqian.turbot.module.ui.marketing

import android.content.Context
import android.content.Intent
import android.util.Log
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.qifenqian.turbot.R
import com.qifenqian.turbot.base.BaseActivity
import com.qifenqian.turbot.config.Constant
import com.qifenqian.turbot.config.TurbotEnum
import com.qifenqian.turbot.module.contract.MarketingContract
import com.qifenqian.turbot.module.entity.response.marketing.ShareDiscountEntity
import com.qifenqian.turbot.module.presenter.MarketingPresenter
import com.qifenqian.turbot.util.Lcat
import com.qifenqian.turbot.util.SpUtils
import kotlinx.android.synthetic.main.activity_share_discount.*
import org.jetbrains.anko.toast
import java.text.SimpleDateFormat

/**
 * @author H
 * @createDate 2019/1/4 11:35
 * @description 分享奖励界面
 * @see
 * @since
 */
class ShareDiscountActivity : BaseActivity<MarketingPresenter, MarketingContract.View>(), MarketingContract.View {

    companion object {
        fun startThis(context: Context) {
            val intent = Intent(context, ShareDiscountActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun bindLayoutId(): Int = R.layout.activity_share_discount

    override fun bindPresenter(): MarketingPresenter? = MarketingPresenter(this, this)

    override fun initial() {
        mPresenter?.getShareDiscount()
        val request = ShareDiscountEntity()
        swi_share_discount.setOnCheckedChangeListener { v, isChecked ->
            if (!v.isPressed) {
                return@setOnCheckedChangeListener
            }
            request.isOpen = if (isChecked) 1 else 0
            ly_share_discount_detail.visibility = if (isChecked) View.VISIBLE else View.GONE
        }
        swi_share_discount_homepage_show.setOnCheckedChangeListener { v, isChecked ->
            if (!v.isPressed) {
                return@setOnCheckedChangeListener
            }
            request.priority = if (isChecked) TurbotEnum.PRIORITY_SHARE.type else mShareDiscountEntity!!.priority
        }
        btn_share_discount_save.setOnClickListener {
            request.conponFaceValue = et_share_discount_price.removeHanziAndLetters().toDouble()
            request.useAmount = et_share_discount_number.text.toString().toInt()
            request.limitOrder = et_share_discount_limit_price.removeHanziAndLetters().toDouble()
            request.startTime = mStartTime
            request.endTime = mEndTime
            request.restaurantId = SpUtils.getInstance().getInt(Constant.SP_KEY_RESTAURANT_ID, -1, Constant.SP_ACCOUNT)
            Lcat.a("ShareDiscountActivity", request)
            mPresenter?.saveShareDiscount(request)
        }
        tv_share_discount_limit_date.setOnClickListener {
            showDatePickDialog(true)
        }
        Lcat.a("ShareDiscountActivity", swi_share_discount.isChecked, ly_share_discount_detail.visibility) //?
    }

    private var mStartTime: String = ""
    private var mEndTime: String = ""
    private fun showDatePickDialog(start: Boolean) {
        val pvTime = TimePickerBuilder(this, OnTimeSelectListener { date, v ->
            if (start) {
                mStartTime = SimpleDateFormat("yyyy-MM-dd").format(date)
                showDatePickDialog(false)
            } else {
                mEndTime = SimpleDateFormat("yyyy-MM-dd").format(date)
                tv_share_discount_limit_date.text = "$mStartTime 至 $mEndTime"
            }
        }).setTimeSelectChangeListener { Log.i("pvTime", "onTimeSelectChanged") }
                .setType(booleanArrayOf(true, true, true, false, false, false))
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build()
        val dialog = pvTime.dialog
        if (dialog != null) {
            val params = FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM
                                                 )
            params.leftMargin = 0
            params.rightMargin = 0
            pvTime.dialogContainerLayout.layoutParams = params
            val dialogWindow = dialog.window
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim)//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM)//改成Bottom,底部显示
            }
        }
        dialog.show()
    }

    private var mShareDiscountEntity: ShareDiscountEntity? = null
    override fun getShareDiscountResult(isSuccess: Boolean, code: Int, msg: String?, shareDiscountEntity: ShareDiscountEntity?) {
        if (isSuccess) {
            mShareDiscountEntity = shareDiscountEntity
            if (shareDiscountEntity?.isOpen == 1) {
                swi_share_discount.isChecked = true
                ly_share_discount_detail.visibility = View.VISIBLE
            } else {
                swi_share_discount.isChecked = false
                ly_share_discount_detail.visibility = View.GONE
            }
            et_share_discount_price.setText("${(shareDiscountEntity?.conponFaceValue!!) / 100}元")
            et_share_discount_number.setText("${shareDiscountEntity.useAmount}")
            et_share_discount_limit_price.setText("${shareDiscountEntity.limitOrder}元")
            tv_share_discount_limit_date.text = "${shareDiscountEntity.startTime} - ${shareDiscountEntity.endTime}"
            swi_share_discount_homepage_show.isChecked = shareDiscountEntity.priority != 0
        } else {
            toast(msg!!)
//            finishDelay()
        }
    }

    override fun saveShareDiscountResult(isSuccess: Boolean, code: Int, msg: String?) {
        if (isSuccess) {
            finishDelay()
        }
        toast(msg!!)
    }

}
